package day4;

import java.lang.reflect.Field;
import java.util.Date;

//反射测试 设置获取单个属性
public
class TestField {
    public static
    void main(String[] args) throws NoSuchFieldException, IllegalAccessException {
        //1.获取类对象
        Class<?> c = Per.class;
        //2.获取单个属性
        Field idField=c.getDeclaredField("id");
        Field nameField=c.getDeclaredField("name");
        Field birthdayField=c.getDeclaredField("birthday");
        System.out.println(idField);
        idField.setAccessible(true);
        //3.设置属性值
        Per p=new Per(10);
        idField.set(p,100);//
        nameField.set(p,"周正");//set(对象，值) 用于设置属性值
        birthdayField.set(p,new Date());
        //4.获取属性值
        System.out.println(p.name+" "+p.birthday);
        //通过反射获取属性值
        //get(对象) 用于获取属性值
        Object value=idField.get(p);
        Object value1 = nameField.get(p);
        Object value2 = birthdayField.get(p);
        System.out.println(value+" "+value1+" "+value2);

    }
}
